# 登陆路由对象
from fastapi import APIRouter
from pydantic import BaseModel

from dao.system.role_dao import Role

login_api = APIRouter()


class LoginResponse:
    role: Role
    userId: int
    username: str
    token: str
    realName: str
    desc: str

    def __init__(self, user_id: int, role: Role, token: str, username: str, desc: str, real_name: str) -> None:
        self.role = role
        self.userId = user_id
        self.username = username
        self.token = token
        self.realName = real_name
        self.desc = desc

    def serialize(self) -> dict:
        return {
            "role": self.role.serialize(),
            "userId": self.userId,
            "username": self.username,
            "token": self.token,
            "realName": self.realName,
            "desc": self.desc,
        }


class Result(BaseModel):
    code: int = 0
    result: dict | None = None
    message: str | None = None
    type: str | None = None

    def __init__(self, result):
        super().__init__()
        self.result = result

    def serialize(self) -> dict:
        return {
            "code": self.code,
            "result": self.result,
            "message": self.message,
            "type": self.type,
        }


class Login(BaseModel):
    username: str
    password: str


def resp_data(result):
    print(result)
    return {
        'code': 0,
        'message': "Success",
        'result': result,
    }


def resp_page(data, total):
    return {
        'code': 0,
        'message': "",
        'result': {
            'items': data,
            'total': total,
        },
    }


def resp():
    return {
        'code': 0,
        'message': "",
        'result': '',
    }


def get_err_resp(result, message="error", code=500):
    return {
        'code': code,
        'message': message,
        'result': result,
    }


userinfo = {
    'roles': [
        {
            'roleName': 'Super Admin',
            'value': 'super',
        },
    ],
    'userId': '1',
    'username': 'vben',
    'token': 'fakeToken1',
    'realName': 'Vben Admin',
    'desc': 'manager',
}


@login_api.post("/login")
def login(login: Login):
    return resp_data(userinfo)


@login_api.get('/getUserInfo')
def user_info():
    return resp_data(userinfo)
